package com.PracticeSSm.controller;

import com.PracticeSSm.utils.VerifyCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author cc
 * @date 2021/8/20 19:04
 * @Description
 */
@Controller
public class CodeController {
    @GetMapping("/Verify")
    public void verify(HttpServletRequest req, HttpServletResponse resp){
        try {
            ServletOutputStream outputStream = resp.getOutputStream();
            VerifyCode verifyCode=new VerifyCode();
            BufferedImage bufferedImage = verifyCode.getImage();
            //系统生成的验证码
            String codeText = verifyCode.getText();
            System.out.println("系统生成的验证码是:"+codeText);
            //把系统生成的验证码存储起来
            HttpSession session = req.getSession();
            session.setAttribute("syscode",codeText);
            //输出一张随机的验证码图片
            verifyCode.output(bufferedImage,outputStream);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
